package com.ecloud.gardensterminal.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.gardensterminal.controller.dto.GardensDeviceAreaDto;
import com.ecloud.gardensterminal.controller.dto.GardensDeviceDto;
import com.ecloud.gardensterminal.controller.vo.GardensDeviceAreaStatusVo;
import com.ecloud.gardensterminal.controller.vo.GardensDeviceAreaVo;
import com.ecloud.gardensterminal.controller.vo.GardensDeviceParallelingInfoVo;
import com.ecloud.gardensterminal.controller.vo.GardensDeviceSwitchInfoVo;
import com.ecloud.gardensterminal.dao.GardensDeviceMapper;
import com.ecloud.gardensterminal.dao.GardensParallelingMapper;
import com.ecloud.gardensterminal.dao.GardensSwitchMapper;
import com.ecloud.gardensterminal.domain.GardensDevice;
import com.ecloud.gardensterminal.domain.GardensParalleling;
import com.ecloud.gardensterminal.domain.GardensSwitch;
import com.ecloud.gardensterminal.server.GardensDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Squbi
 */
@Service
public class GardensDeviceServiceImpl extends ServiceImpl<GardensDeviceMapper, GardensDevice> implements GardensDeviceService {
    @Autowired
    private GardensDeviceMapper deviceMapper;

    @Autowired
    private GardensSwitchMapper switchMapper;

    @Autowired
    private GardensParallelingMapper parallelingMapper;

    @Override
    public GardensDevice selectById(Integer deviceId) {
        return deviceMapper.selectById(deviceId);
    }

    @Override
    public List<GardensDevice> selectAll(GardensDeviceDto dto) {
        dto.setPageIndex(dto.getPageSize() * (dto.getPageIndex() - 1));
        return deviceMapper.selectByDto(dto);
    }

    @Override
    public List<GardensDeviceAreaVo> selectJoinArea(GardensDeviceAreaDto dto) {
        return deviceMapper.selectJoinArea(dto);
    }

    @Override
    public List<GardensDeviceAreaStatusVo> selectJoinAreaStatus(GardensDeviceAreaDto dto) {
        ArrayList<GardensDeviceAreaStatusVo> result = new ArrayList<>();
        List<GardensDeviceAreaVo> gardensDeviceAreaVos = selectJoinArea(dto);
        for (GardensDeviceAreaVo gardensDeviceAreaVo : gardensDeviceAreaVos) {
            ArrayList<Boolean> booleans = new ArrayList<>();
            GardensDeviceAreaStatusVo gda = new GardensDeviceAreaStatusVo(gardensDeviceAreaVo, booleans);
            if (gardensDeviceAreaVo.getDeviceType() == 4) {
                GardensSwitch gardensSwitch = switchMapper.selectNewly(gardensDeviceAreaVo.getDeviceId());
                if (gardensSwitch != null) {
                    booleans.add(gardensSwitch.getSwitchOpen());
                }
            } else if (gardensDeviceAreaVo.getDeviceType() == 5) {
                GardensParalleling gardensParalleling = parallelingMapper.selectNewly(gardensDeviceAreaVo.getDeviceId());
                if (gardensParalleling != null) {
                    booleans.add(gardensParalleling.getParallelingOpen1());
                    booleans.add(gardensParalleling.getParallelingOpen2());
                    booleans.add(gardensParalleling.getParallelingOpen3());
                }
            }
            result.add(gda);
        }
        return result;
    }

    @Override
    public List<GardensDeviceSwitchInfoVo> selectJoinSwitch(Integer deviceId) {
        List<GardensDeviceSwitchInfoVo> result = new ArrayList<>();
        List<GardensDeviceAreaVo> deviceAreaVos = selectJoinArea(new GardensDeviceAreaDto(4, 0, deviceId));
        for (GardensDeviceAreaVo deviceAreaVo : deviceAreaVos) {
            GardensSwitch gardensSwitch = switchMapper.selectNewly(deviceAreaVo.getDeviceId());
            result.add(new GardensDeviceSwitchInfoVo(deviceAreaVo, gardensSwitch.getSwitchOpen(), gardensSwitch.getSwitchTime()));
        }
        return result;
    }

    @Override
    public List<GardensDeviceParallelingInfoVo> selectJoinParalleling(Integer deviceId) {
        List<GardensDeviceParallelingInfoVo> result = new ArrayList<>();
        List<GardensDeviceAreaVo> deviceAreaVos = selectJoinArea(new GardensDeviceAreaDto(5, 0, deviceId));
        for (GardensDeviceAreaVo deviceAreaVo : deviceAreaVos) {
            GardensParalleling gardensParalleling = parallelingMapper.selectNewly(deviceAreaVo.getDeviceId());
            result.add(new GardensDeviceParallelingInfoVo(deviceAreaVo, gardensParalleling));
        }
        return result;
    }
}
